package com.ajk.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ajk.server.model.EaseUser;
import com.ajk.server.model.query.EaseUserQuery;
/**
 * 环信用户  <br>
 * t_ease_user <br>
 * v1.0 2016-05-06
 * @see com.ajk.server.dao.EaseUserMapper
 */
 public class EaseUserProvider {
 
 	/**
 	* 新增 环信用户
 	*/
 	public String insert(EaseUser domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into t_ease_user(create_time,last_modify_time");
	 	if(domain.getUserId()!=null){
			sb.append(",user_id");
		}
	 	if(domain.getUsername()!=null){
			sb.append(",username");
		}
	 	if(domain.getPassword()!=null){
			sb.append(",password");
		}
	 	if(domain.getNickname()!=null){
			sb.append(",nickname");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getUserId()!=null){
			sb.append(",#{userId}");
		}
	 	if(domain.getUsername()!=null){
			sb.append(",#{username}");
		}
	 	if(domain.getPassword()!=null){
			sb.append(",#{password}");
		}
	 	if(domain.getNickname()!=null){
			sb.append(",#{nickname}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 环信用户
 	*/
	public String update(EaseUser domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_ease_user set last_modify_time = now() ");
		
		if(domain.getUserId()!=null){
			sb.append(",user_id=#{userId}");
		}
		if(domain.getUsername()!=null){
			sb.append(",username=#{username}");
		}
		if(domain.getPassword()!=null){
			sb.append(",password=#{password}");
		}
		if(domain.getNickname()!=null){
			sb.append(",nickname=#{nickname}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<EaseUser>> map){
		StringBuffer sb = new StringBuffer();
		List<EaseUser> easeUsers = map.get("list");
		sb.append("insert into t_ease_user(create_time,last_modify_time,user_id,username,password,nickname) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].userId}"
	    		+ ",#'{'list[{0}].username}"
	    		+ ",#'{'list[{0}].password}"
	    		+ ",#'{'list[{0}].nickname}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(easeUsers)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  easeUsers.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 环信用户
 	*/
	public String findByQuery(EaseUserQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_ease_user a ");
		
		//TODO: add your logic
		
		if(StringUtils.isEmpty(query.getSortStatment())){
			sb.append(" order by create_time desc ");
		}else{
			sb.append(" ").append(query.getSortStatment());
		}
		if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(EaseUserQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_ease_user a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 环信用户
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_ease_user where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 	public String findByUserIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_ease_user where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" user_id=").append("#{list[0]}");
			}else{
				sb.append(" user_id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
 	
 }	